/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.gl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import src.domain.gl.Glbalance;
import src.services.gl.GledgerModel;
import src.services.system.BranchModel;
import src.util.Utils;

/**
 *
 * @author Lalugina
 */
@ManagedBean
@SessionScoped
public class GlbalanceBean {

    /**
     * Creates a new instance of GlbalanceBean
     */
    private List<SelectItem> avblBranch, avblStyle, avblPeriod;
    private List<Glbalance> glbalances;
    private Date startDate, endDate, maxDate, minDate;
    private Integer branchId, period, year, styleId;
    
    public GlbalanceBean() {
        avblStyle = new ArrayList<>();
        avblStyle.add(new SelectItem(1, "Period"));
        avblStyle.add(new SelectItem(2, "Bulan"));
        
        this.styleId = 1;
        this.avblBranch = new BranchModel().listAll();
        this.avblPeriod = Utils.getMonths();
        this.glbalances = new ArrayList<>();
        
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        startDate = cal.getTime();
        minDate = cal.getTime();
        
        Calendar cal2 = Calendar.getInstance();;
        int lastDate = cal2.getActualMaximum(Calendar.DATE);
        cal2.set(Calendar.DATE, lastDate);
        endDate = cal2.getTime();
        maxDate = cal2.getTime();
        
        period = cal.get(Calendar.MONTH)+1;
        year = cal.get(Calendar.YEAR);
        
    }

    public List<SelectItem> getAvblPeriod() {
        return avblPeriod;
    }

    public List<SelectItem> getAvblBranch() {
        return avblBranch;
    }

    public List<SelectItem> getAvblStyle() {
        return avblStyle;
    }

    public Date getMaxDate() {
        return maxDate;
    }

    public Date getMinDate() {
        return minDate;
    }

    public Integer getBranchId() {
        return branchId;
    }

    public void setBranchId(Integer branchId) {
        this.branchId = branchId;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Integer getPeriod() {
        return period;
    }

    public void setPeriod(Integer period) {
        this.period = period;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Integer getStyleId() {
        return styleId;
    }

    public void setStyleId(Integer styleId) {
        this.styleId = styleId;
        System.out.println(this.styleId);
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
        
    }

    public List<Glbalance> getGlbalances() {
        return glbalances;
    }
    
    public String selectItemValueOf(Integer id) {
        return (String) Utils.selectItemValueOf(avblPeriod, id);
    }
    
    public void changeDate() {
        if (this.styleId==1) {
            Calendar cal = Calendar.getInstance();
            cal.clear();
            cal.set(year, period-1, Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, 1);

            startDate = cal.getTime();
            minDate = cal.getTime();

            Calendar cal2 = Calendar.getInstance();
            cal2.clear();
            cal2.set(year, period-1, Calendar.DAY_OF_MONTH);
            int lastDate = cal2.getActualMaximum(Calendar.DATE);
            cal2.set(Calendar.DATE, lastDate);

            endDate = cal2.getTime();
            maxDate = cal2.getTime();
        }
    }
    
    public void loadData() {
        if (styleId==1) 
            this.glbalances = new GledgerModel().loadDataGLBalancePeriod(branchId, startDate, endDate);
        else this.glbalances = new GledgerModel().loadDataGLBalanceMonth(branchId, year);
    }
}
